package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.LogisticsAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.BuyerOrderDetailBean;
import com.yiwei.zxp.api.bean.IdBean;
import com.yiwei.zxp.api.model.ExpressCheckModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class LogisticsActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_goods)
    ImageView imgGoods;
    @BindView(R.id.txt_goods_num)
    TextView txtGoodsNum;
    @BindView(R.id.txt_left_info)
    TextView txtLeftInfo;
    @BindView(R.id.txt_right_info)
    TextView txtRightInfo;
    @BindView(R.id.list)
    RecyclerView list;

    private Unbinder unbiner;
    private int x26;
    private int x28;
    private LogisticsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        x26 = getResources().getDimensionPixelSize(R.dimen.x26);
        x28 = getResources().getDimensionPixelSize(R.dimen.x28);
        txtTitle.setText("查看物流");
        adapter = new LogisticsAdapter(this);
        list.setAdapter(adapter);
        String order_no = getIntent().getStringExtra("order_no");
        getExpressCheck(order_no);
    }

    public void getExpressCheck(String order_no) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        BuyerOrderDetailBean bean = new BuyerOrderDetailBean();
        bean.order_no = order_no;

        Api.api_service.getExpressCheck(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ExpressCheckModel.DatasBean item = result.datas;
                    Spanned txt_left = Spans.with(this).font("物流状态", x28, 0xff000000).
                            font("\n物流公司：", x26, 0xff999999).
                            font("\n运单编号：", x26, 0xff999999).
                            font("\n官方电话：", x26, 0xff999999).
                            build();
                    txtLeftInfo.setText(txt_left);
                    Spanned txt_right = Spans.with(this).font(getStatus(item.status), x28, 0xff4CD127).
                            font("\n" + item.express_name, x26, 0xff999999).
                            font("\n" + item.express_no, x26, 0xff999999).
                            font("\n" + item.express_phone, x26, 0xff3CB2EF).
                            build();
                    txtRightInfo.setText(txt_right);
                    txtGoodsNum.setText("共" + item.order_count + "件商品");
                    if (item.order_photos != null && item.order_photos.size() != 0) {
                        Glide.with(this).load(item.order_photos.get(0)).into(imgGoods);

                    }
                    adapter.update(item.result);
                });
    }
    //快递状态（*  1.在途中 2.正在派件 3.已签收 4.派送失败  *）

    private String getStatus(String status) {
        switch (status) {
            case "1":
                return "在途中";
            case "2":
                return "正在派件";
            case "3":
                return "已签收";
            case "4":
                return "派送失败";
            default:
                return "";
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
